package com.example.teesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.teesystem.common.cache.CourseCache;
import com.example.teesystem.common.cache.UserCache;
import com.example.teesystem.common.utils.Response;
import com.example.teesystem.common.utils.ResultCode;
import com.example.teesystem.entity.Course;
import com.example.teesystem.entity.IssueFeedback;
import com.example.teesystem.entity.User;
import com.example.teesystem.entity.vo.IssueFeedback.IssueFeedbackExcel;
import com.example.teesystem.entity.vo.IssueFeedback.IssueFeedbackInsertVo;
import com.example.teesystem.entity.vo.IssueFeedback.IssueFeedbackSelectVo;
import com.example.teesystem.entity.vo.PageResponseVo;
import com.example.teesystem.entityDto.IssueFeedbackDto;
import com.example.teesystem.filter.MyHandlerInterceptor;
import com.example.teesystem.service.IssueFeedbackService;
import com.example.teesystem.mapper.IssueFeedbackMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author 12510
* @description 针对表【t_issue_feedback】的数据库操作Service实现
* @createDate 2022-03-31 16:24:34
*/
@Service
@RequiredArgsConstructor
public class IssueFeedbackServiceImpl extends ServiceImpl<IssueFeedbackMapper, IssueFeedback>
    implements IssueFeedbackService{
    private final IssueFeedbackMapper issueFeedbackMapper;

    @Override
    public Response submit(IssueFeedbackInsertVo issueFeedbackInsertVo) {
        IssueFeedback issueFeedback = new IssueFeedback();
        BeanUtils.copyProperties(issueFeedbackInsertVo,issueFeedback);
        issueFeedback.setUploadUid(MyHandlerInterceptor.getUid());
        try{
            issueFeedbackMapper.insert(issueFeedback);
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.INSERT_ERROR.getMsg(), ResultCode.INSERT_ERROR.getCode());
        }
    }

    @Override
    public PageResponseVo<IssueFeedbackDto> getIssueFeedback(IssueFeedbackSelectVo issueFeedbackSelectVo){
        Page<IssueFeedback> coursePage = new Page<>(issueFeedbackSelectVo.getCur(),issueFeedbackSelectVo.getSize());
        QueryWrapper<IssueFeedback> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",issueFeedbackSelectVo.getCourseId())
                .eq("is_handle",issueFeedbackSelectVo.getIsHandle());
        Page<IssueFeedback> myPage = this.page(coursePage,wrapper);
        List<IssueFeedback> issueFeedbackList = myPage.getRecords();
        List<IssueFeedbackDto> issueFeedbackDtoList = new ArrayList<>();
        Course courseDef = new Course();
        courseDef.setName("课程未找到");
        Course course = CourseCache.courseMap.getOrDefault(issueFeedbackSelectVo.getCourseId(),courseDef);
        User def = new User();
        def.setMyName("用户未找到");
        for (IssueFeedback issueFeedback : issueFeedbackList) {
            IssueFeedbackDto issueFeedbackDto = new IssueFeedbackDto(issueFeedback);
            User u1 = UserCache.userMap.getOrDefault(issueFeedback.getUploadUid(), def);
            if(issueFeedbackDto.getHandlerId()!=null){
                User u2 = UserCache.userMap.getOrDefault(issueFeedback.getHandlerId(), def);
                issueFeedbackDto.setHandlerName(u2.getMyName());
            }
            issueFeedbackDto.setCourseName(course.getName());
            issueFeedbackDto.setUploadName(u1.getMyName());
            issueFeedbackDtoList.add(issueFeedbackDto);
        }
        return new PageResponseVo<>(issueFeedbackDtoList, myPage.getCurrent(), myPage.getSize(), myPage.getPages());
    }

    @Override
    public Response handleFeedback(int uid, int id) {
        try{
            issueFeedbackMapper.handleFeedback(uid,id);
            return Response.ok();
        }catch (Exception e){
            return Response.error(ResultCode.UPDATE_MESSAGE_ERROR.getMsg(), ResultCode.UPDATE_MESSAGE_ERROR.getCode());
        }
    }

    @Override
    public List<IssueFeedbackExcel> getIssueFeedbackExcelList(int isHandle, int courseId) {
        List<IssueFeedback> issueFeedbackList = issueFeedbackMapper.getIssueFeedbackList(isHandle,courseId);
        List<IssueFeedbackExcel> issueFeedbackExcelList = new ArrayList<>();
        User u = new User();
        Course course = new Course();
        u.setMyName("未找到该用户");
        course.setName("未找到该课程");
        for (IssueFeedback issueFeedback : issueFeedbackList) {
            IssueFeedbackExcel issueFeedbackExcel = new IssueFeedbackExcel(issueFeedback);
            if(issueFeedback.getHandlerId()!=null){
                User handler = UserCache.userMap.getOrDefault(issueFeedback.getHandlerId(),u);
                issueFeedbackExcel.setHandlerName(handler.getMyName());
            }
            User uploader = UserCache.userMap.getOrDefault(issueFeedback.getUploadUid(),u);
            issueFeedbackExcel.setUploadName(uploader.getMyName());
            Course myCourse = CourseCache.courseMap.getOrDefault(issueFeedback.getCourseId(),course);
            issueFeedbackExcel.setCourseName(myCourse.getName());
            issueFeedbackExcelList.add(issueFeedbackExcel);
        }
        return issueFeedbackExcelList;
    }
}




